<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10 paddingBottom20">
      <div class="mainor_form">
        <div class="mainor_te"></div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="单号" prop="order_sn">
            <el-input
              v-model.trim="form.order_sn"
              placeholder="账单号/批发单号"
              clearable
              class="wid220"
            ></el-input>
          </el-form-item>
          <el-form-item label="账单日期" prop="account_order_time">
            <el-date-picker
              v-model="account_order_time"
              type="daterange"
              class="time220"
              :editable="false"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :clearable="false"
              :picker-options="pickerOptions"
            />
          </el-form-item>

          <el-form-item label="对账状态" prop="account_status">
            <el-select
              v-model.trim="form.account_status"
              placeholder="对账状态"
              class="wid220"
            >
              <el-option label="所有" :value="parseInt(0)"></el-option>
              <el-option
                v-for="(account_status_list, index) in account_status_list"
                :key="index"
                :label="account_status_list"
                :value="parseInt(index) + 1"
              ></el-option>
            </el-select>
          </el-form-item>
          <div class="more el-form-item" @click="more">
            <span>更多</span>
            <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
          </div>
          <div class="clearfix"></div>
          <el-collapse-transition>
            <div v-if="moredata">
              <div>
                <el-form-item label="账期" prop="payment_days">
                  <el-date-picker
                    v-model="form.payment_days"
                    type="month"
                    placeholder="选择月"
                    value-format="yyyy-MM"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="批发商" prop="wholesaler">
                  <el-select
                    v-model.trim="form.wholesaler"
                    placeholder="批发商"
                    class="wid220"
                  >
                    <el-option label="所有" value="0"></el-option>
                    <el-option
                      v-for="(group, index) in companies"
                      :key="index"
                      :label="group"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="创建日期">
                  <el-date-picker
                    v-model="createTime"
                    type="daterange"
                    class="time220"
                    :editable="false"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd"
                    :clearable="false"
                    :picker-options="pickerOptions"
                  />
                </el-form-item>
                <el-form-item label="结算状态" prop="status">
                  <el-select
                    v-model.trim="form.status"
                    placeholder="结算状态"
                    class="wid220"
                  >
                    <el-option label="所有" :value="parseInt(0)"></el-option>
                    <el-option
                      v-for="(group, index) in settle_status_list"
                      :key="index"
                      :label="group"
                      :value="parseInt(index) + 1"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </div>
          </el-collapse-transition>

          <el-form-item class="marginTop10">
            <el-button class="reset marginLeft10" type="primary" @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="main_table" style="min-height: 570px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :header-cell-class-name="this.$verticaline"
        >
          <el-table-column
            label="批发单号"
            prop="order_sn"
            :show-overflow-tooltip="true"
            width="220"
          />
          <el-table-column label="批发商" prop="" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ scope.row.dispatch ? scope.row.dispatch.company_name : "" }}
            </template>
          </el-table-column>
          <el-table-column
            label="配送金额"
            prop="send_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="下单金额"
            prop="order_money"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="账单号"
            prop="related_order_sn"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="账单金额"
            prop="account_money"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="差异金额"
            prop="diff_money"
            :render-header="renderHeader_purchase"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="对账状态"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span :class="'account_status' + scope.row.account_status">
                {{ account_status_list[scope.row.account_status] }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="结算状态"
            prop=""
            :show-overflow-tooltip="true"
            ><template slot-scope="scope">
              <span :class="'settle_status' + scope.row.status">
                {{ settle_status_list[scope.row.status] }}
              </span>
            </template></el-table-column
          >
          <el-table-column
            label="账期"
            prop="payment_days"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="账单日期"
            prop=""
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">{{
              scope.row.account_time
                ? parseTime(scope.row.account_time, "{y}-{m}-{d}")
                : ""
            }}</template>
          </el-table-column>

          <el-table-column
            label="管理操作"
            align="left"
            fixed="right"
            width="240"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <router-link :to="'/bulk/account/see/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <el-link type="warning" @click="exportb(scope.row.id)"
                >导出</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { bulk_account, bulk_account_export } from "@/api/bulk/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime, exportMethod } from "@/utils/index";
import dateOption from "@/options/dateOption";

export default {
  data() {
    return {
      parseTime,
      exportMethod,
      createTime: [],
      account_order_time: [],
      pickerOptions: dateOption,
      // 搜索
      form: {
        order_sn: "", //单号
        account_order_start: "", //账单日期
        account_order_end: "",
        account_status: "", //对账状态
        payment_days: "", //账期
        wholesaler: "", //批发商
        create_start_time: "", //创建日期
        create_end_time: "",
        status: "", //结算状态
      },

      // 更多
      moredata: false,
      // 列表数据
      list: [],
      listLoading: false,
      account_status_list: [],
      settle_status_list: [],
      companies: [],
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
    };
  },
  methods: {
    // 订单差额 ？注释
    renderHeader_purchase(h, { column, $index }) {
      // 这里在最外层插入一个div标签
      return h("div", [
        // h即为cerateElement的简写
        // 在div里面插入span
        h("span", {
          // 表示内容
          domProps: {
            innerHTML: column.label,
          },
          on: {
            click: () => {
              console.log(`${column.label}   ${$index}`);
            },
          },
        }),
        h(
          "el-tooltip",
          {
            // 表示属性
            attrs: {
              effect: "dark",
              content: "注释：差异金额=下单金额-账单金额",
              placement: "top",
            },
          },
          [
            h("i", {
              style: "margin-left: 5px;margin-top:-1px;",
              class: "el-icon-question table-msg",
            }),
          ]
        ),
      ]);
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.bulk_account();
    },
    changePage(page) {
      this.current.page = page;
      this.bulk_account();
    },
    // 导出明细
    exportb(id) {
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      bulk_account_export(data).then((res) => {
        this.exportMethod(res, "批发账单详情", notify);
      });
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.bulk_account();
    },
    //重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.account_order_time = [];
      if (this.moredata) {
        this.createTime = [];
      }
      this.bulk_account();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    bulk_account() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      bulk_account(data).then((res) => {
        if (res.errcode == 0) {
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
          this.account_status_list = res.data.account_status_list;
          this.settle_status_list = res.data.settle_status_list;
          this.companies = res.data.companies;
          this.listLoading = false;
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));

      form.account_order_start = this.account_order_time
        ? this.account_order_time[0]
        : "";
      form.account_order_end = this.account_order_time
        ? this.account_order_time[1]
        : "";
      form.create_start_time = this.createTime ? this.createTime[0] : "";
      form.create_end_time = this.createTime ? this.createTime[1] : "";
      return form;
    },
    list_data() {
      let arry = [];
      let list = JSON.parse(JSON.stringify(this.list.list));

      return JSON.stringify(arry);
    },
  },
  created() {
    // 列表
    this.bulk_account();
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.mainor_form form {
  width: calc(100% - 280px);
}

.account_status0 {
  color: #eb0a0a;
}
.account_status1 {
  color: #03a98e;
}
.settle_status0 {
  color: #eb0a0a;
}
.settle_status1 {
  color: #03a98e;
}
</style>
